<template>
	<view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">名称</view>
				<input placeholder="请输入商品名" name="input" v-model="title"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">型号</view>
				<input placeholder="多个型号用分号分隔,如: 蓝色;白色" name="input" v-model="attr"></input>
			</view>

			<view class="cu-form-group">
				<view class="title">所需积分数</view>
				<input placeholder="请输入所需积分数" name="input" type="number" v-model.number="scores"></input>
			</view>

			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<view class="bg-white padding">
				<text class="gray">第一张图为商品目录图片</text>
			</view>
			<view class="cu-form-group padding">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>

			<!-- !!!!! placeholder 在ios表现有偏移 建议使用 第一种样式 -->
			<view class="cu-form-group margin-top">
				<textarea maxlength="-1" @input="textareaInput" placeholder="输入商品详细描述"></textarea>
			</view>
			<view class="margin-top">
				<button class="bg-blue" @click="submit">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	import upFile from '@/common/uploadUtils.js'
	
	const app = getApp();
	export default {
		data() {
			return {
				scores: 0,
				title: '',
				imgList: [],
				desc: '',
				imgPaths: [],
				attr:'',
				userInfo: null
			};
		},
		
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo');
		},
		
		methods: {
			ChooseImage(e) {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					content: '确定要删除这张照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			textareaInput(e) {
				this.desc = e.detail.value
			},
			// 提交活动申请
			submit: function(e) {
				var that = this;
				if (this.title && this.imgList && this.scores && this.attr) {
					var file_path = new Array();
					upFile.mutiupload(this.imgList, file_path ,0, this.imgList.length, this.title, function(file_path) {
						var createDate = new Date();
						that.attr = that.attr.split((/;|；/)) // 正则匹配全角、半角负号
						const addData = {
												title: that.title,
												imgPath: file_path,
												scores: that.scores,
												creator: that.userInfo.wx_openid,
												createDate: createDate.toISOString().slice(0,10),
												desc: that.desc,
												attr: that.attr,
												onShow: true
											}
						uniCloud.database().collection('mall-gifts')
						.add(addData)
						.then(() => {
								uni.hideLoading();
								uni.showToast({
									title: '提交成功',
									success: () => {
										setTimeout(() => {
											uni.navigateBack({
												delta: 1
											})
										}, 1500)
									}
								})
							})
						.catch((err) => {
								console.log(err)
							})
					})
				} else {
					uni.showToast({
						icon: "none",
						title: '请完整填写商品信息'
					})
				}

			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
